//
//  RBStockOrderListCell.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/17.
//

#import "RBStockOrderListCell.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RBPager.h"
#import "RanTool.h"

@interface RBStockOrderListCell()

@property (nonatomic, strong) UIView *containerView;

@property (nonatomic, strong) UIView *shopView;
@property (nonatomic, strong) UILabel *shopNameL;
@property (nonatomic, strong) UILabel *statusL;

@property (nonatomic, strong) RBCellModel *cellModel;

@end

@implementation RBStockOrderListCell

- (void)setCellModel:(RBCellModel *)cellModel
{
    _cellModel = cellModel;
    RBOrderListModel *model = cellModel.cellData;
    //
    [self.containerView view_clear];
    //店铺
    [self.containerView view_addView:self.shopView withHeight:Zoom(43) width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
    [self.containerView view_addView:[self lineView] withHeight:1 width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(15) right:Zoom(15) centerX:0];
    //店铺名称
    self.shopNameL.text = model.store_name;
    //订单状态
    NSString *statusString = @"";
    if ([model.order_status isEqualToString:@"0"]) {
        statusString = @"待付款";
    } else {
        statusString = @"已付款";
    }
    self.statusL.text = statusString;
    //商品
    [self.containerView view_addView:[self goodsViewWithData:model] withHeight:Zoom(160) width:CGFLOAT_MIN top:Zoom(13) bottom:0 left:Zoom(0) right:Zoom(0) centerX:0];
}

#pragma mark 点击
//查看订单
- (void)clickCheckOrder {
    if (self.cellModel.eventBlock1)
    {
        self.cellModel.eventBlock1(0, 1, self.cellModel.cellData);
    }
}
//加工订单
- (void)clickAddOrder {
    if (self.cellModel.eventBlock1)
    {
        self.cellModel.eventBlock1(0, 2, self.cellModel.cellData);
    }
}
//支付
- (void)clickPayOrder {
    if (self.cellModel.eventBlock1)
    {
        self.cellModel.eventBlock1(0, 3, self.cellModel.cellData);
    }
}


#pragma mark - Getter

- (UIView *)containerView
{
    if (!_containerView)
    {
        self.containerView = ({
            UIView *view = UIView.new;
            view.backgroundColor = UIColor.whiteColor;
            [view rbSetCornerRadius:Zoom(8)];
            [self.contentView addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(0);
                make.width.mas_equalTo(Zoom(350));
            }];
            view;
        });
    }
    return _containerView;
}

- (UIView *)shopView
{
    if (!_shopView)
    {
        self.shopView = ({
            UIView *view = UIView.new;
            
            UIImageView *shopImgView = [RanTool imgViewWithMode:1 radius:0 imgName:@"groupPayShop"];
            [view addSubview:shopImgView];
            [shopImgView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(Zoom(19));
                make.height.mas_equalTo(Zoom(17));
                make.left.mas_equalTo(Zoom(16));
                make.centerY.mas_equalTo(0);
            }];
            
            UILabel *shopNameL = [RanTool labelWithFontSize:Zoom(15) weight:1 color:[RBThemeConfig textBlackColor] align:0 text:@"店铺名称"];
            self.shopNameL = shopNameL;
            [view addSubview:shopNameL];
            [shopNameL mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(0);
                make.width.mas_equalTo(Zoom(160));
                make.left.mas_equalTo(shopImgView.mas_right).offset(Zoom(11));
            }];
            
            UILabel *statusL = [RanTool labelWithFontSize:Zoom(14) weight:2 color:[UIColor colorWithHexString:@"#518CFF"] align:0 text:@"status"];
            self.statusL = statusL;
            [view addSubview:statusL];
            [statusL mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(-Zoom(12));
                make.centerY.mas_equalTo(0);
            }];
            
            view;
        });
    }
    return _shopView;
}

- (UIView *)lineView
{
    UIView *view = UIView.new;
    view.backgroundColor = [RBThemeConfig lineGrayColor];
    return view;
}

- (UIView *)goodsViewWithData: (RBOrderListModel *)data
{
    UIView *view = UIView.new;
    
    UIImageView *imgView = [RanTool imgViewWithMode:2 radius:Zoom(5) imgName:data.file];
    [view addSubview:imgView];
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(Zoom(15));
        make.top.mas_equalTo(0);
        make.width.height.mas_equalTo(Zoom(97));
    }];
    
    UILabel *nameL = [RanTool labelWithFontSize:Zoom(14) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:data.goods_name];
    nameL.numberOfLines = 2;
    [view addSubview:nameL];
    [nameL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(imgView.mas_right).offset(Zoom(20));
        make.top.mas_equalTo(0);
        make.right.mas_equalTo(-Zoom(15));
    }];
    
    UILabel *countL = [RanTool labelWithFontSize:Zoom(12) weight:0 color:[RBThemeConfig textGrayColor] align:2 text:[NSString stringWithFormat:@"x%@", data.quantity]];
    [countL setContentHuggingPriority:UILayoutPriorityDefaultHigh + 1 forAxis:UILayoutConstraintAxisHorizontal];
    [countL setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh + 1 forAxis:UILayoutConstraintAxisHorizontal];
    [view addSubview:countL];
    [countL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(nameL);
        make.top.mas_equalTo(nameL.mas_bottom).offset(Zoom(14));
        
    }];
    
    UILabel *priceL = UILabel.new;
    [view addSubview:priceL];
    [priceL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(nameL);
        make.top.mas_equalTo(countL.mas_bottom).offset(Zoom(12));
    }];
    priceL.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
        [maker addAttributeStringWithFont:[UIFont boldSystemFontOfSize:Zoom(11)] color:[RBThemeConfig priceRedColor] string:@"¥"];
        [maker addAttributeStringWithFont:[UIFont boldSystemFontOfSize:Zoom(15)] color:[RBThemeConfig priceRedColor] string:data.total_price];
    }];
    
    UIView *bottomView = UIView.new;
    [view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-Zoom(15));
        make.top.mas_equalTo(imgView.mas_bottom);
        make.bottom.mas_equalTo(0);
    }];
    
    [bottomView h_view_addView:({
        UILabel *btn = [RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textGrayColor] align:1 text:@"查看订单" bgColor:UIColor.whiteColor radius:Zoom(15)];
        [btn rbSetBorderWitdh:1 borderColor:[RBThemeConfig textGrayColor]];
        [btn rbAddTapGesWithTarget:self action:@selector(clickCheckOrder)];
        btn;
    }) withHeight:Zoom(30) width:Zoom(75) top:CGFLOAT_MIN bottom:CGFLOAT_MIN left:0 right:0 centerY:YES];
    
    [bottomView h_view_addView:({
        UILabel *btn = [RanTool labelWithFontSize:Zoom(13) weight:0 color:UIColor.whiteColor align:1 text:@"加工下单" bgColor:[RBThemeConfig buttonBlueColor] radius:Zoom(15)];
        [btn addPriority:1];
        if ([data.order_status isEqualToString:@"0"])
        {
            btn.text = @"支付";
            [btn rbAddTapGesWithTarget:self action:@selector(clickPayOrder)];
        } else {
            btn.text = @"加工下单";
            [btn rbAddTapGesWithTarget:self action:@selector(clickAddOrder)];
        }
        btn;
    }) withHeight:Zoom(30) width:Zoom(75) top:CGFLOAT_MIN bottom:CGFLOAT_MIN left:Zoom(15) right:0 centerY:YES];
    
    return view;
}

@end
